<template>
	<div id="login">
		<div id="header">
			<img @click="$router.go(-1)" src="../../static/img/return.png"/>
			<p>登录</p>
			<router-link to="/register" id="toRegister">新用户注册</router-link>
		</div>
		<img id="logo" src="../../static/img/logo-01.png"/>
		<div class="login_item">
			<img src="../../static/img/login-01.png"/>
			<input type="tel" v-model="user" placeholder="请输入手机号" />
		</div>
		<div class="login_item">
			<img src="../../static/img/login-02.png"/>
			<input type="password" v-model="Password" placeholder="请输入密码" />
		</div>
		
		<div class="login_item">
			<img src="../../static/img/login-2.png"/>
			<input type="text" v-model="code" placeholder="请输入验证码" />
			<div class="code" @click="refreshCode">
	    		<identify :identifyCode="identifyCode"></identify>
	    	</div>
		</div>
		
		<input id="sub" @click="login" type="button" value="登录" />
		<div id="toForget">
			<router-link to="/forget">忘记密码</router-link>
		</div>
	</div>
</template>

<script>

export default{
	name: 'login',
	data:function(){
		return{
			user: '',
			Password: '',
			identifyCodes: '1234567890',
			identifyCode: '',	// 图片验证码
			code: ''	// 用户输入的验证码
		}
	},
	mounted: function() {
	    this.identifyCode = "";
	    this.makeCode(this.identifyCodes, 4);
	},
	methods:{
		login: function(){
			var reg=11 && /^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/;
			if(!reg.test(this.user)){
				this.$alert('请输入正确的手机号码');
				this.refreshCode();
				return;
			}
			if(this.code != this.identifyCode){
				this.$alert("验证码不正确");
				this.refreshCode();
				return;
			}
			var _this = this;
			this.$loading();
			let postData = _this.$qs.stringify({
			    username:_this.user,
			    password:_this.Password
			});
			_this.$axios.post('/user/autho/public/authority',postData).then(function(res){
			    if(res.data.message == 'succ'){
			    	localStorage.setItem(`usertoken`,res.data.result);
					let redirect = decodeURIComponent(_this.$route.query.redirect || '/');
					if(redirect == "forget"){
						redirect = "/";
					}
			        _this.$router.replace({
			            path: redirect
			        })
			    }else{
			    	_this.$alert('账号或密码错误');
			    	_this.refreshCode();
			    }
				$("#loading").remove();
			}).catch(function (error) {
			    console.log(error);
				$("#loading").remove();
			});
		},
		randomNum: function(min, max) {
	      return Math.floor(Math.random() * (max - min) + min);
	    },
	    refreshCode: function() {
	      this.identifyCode = "";
	      this.makeCode(this.identifyCodes, 4);
	    },
	    makeCode: function(o, l) {
	      for (let i = 0; i < l; i++) {
	        this.identifyCode += this.identifyCodes[
	          this.randomNum(0, this.identifyCodes.length)
	        ];
	      }
	    }
	},
	created: function(){
  		$("#loading").remove();
	}
}
</script>

<style scoped>
/* ========== */
/* = Header = */
/* ========== */
.code {
  margin: 400px auto;
  width: 114px;
  height: 40px;
}
#header{
	width: 100%;
	background-color: #fff;
	position: fixed;
	top: 0;
	left: 0;
	height: 4rem;
	display: flex;
	align-items: center;
	justify-content: center;
	border-bottom: 1px solid #EEEEEE;
	font-size: 1.6rem;
	color: #4F4F4F;
	box-sizing: border-box;
	z-index: 10;
}
#header img{
	height: 2rem;
	display: block;
	position: absolute;
	top: 1rem;
	left: 2rem;
}
#header a{
	height: 4rem;
	line-height: 4rem;
}
#login{
	height: 100vh;
	padding-top: 5rem;
	box-sizing: border-box;
	background-color: #fff;
}
#toRegister{
	color: #FF6565;
	font-size: 1.2rem;
	position: absolute;
	right: 2rem;
}
#logo{
	width: 40%;
	display: block;
	margin: 4rem auto;
}
.login_item{
	width: 90%;
	margin: 0 auto;
	display: flex;
	align-items: center;
	border-bottom: 1px solid #EEEEEE;
	height: 4rem;
	padding: 0 1rem;
	box-sizing: border-box;
}
.login_item img{
	height: 1.4rem;
	display: block;
	margin-right: 1rem;
}
.login_item input{
	width: calc(100% - 2.4rem);
	border: none;
	height: 100%;
	font-size: 1.2rem;
}
#sub{
	width: 80%;
	margin: 0 auto;
	height: 4rem;
	border-radius: 50px;
	background-color: #FF6565;
	color: #FFFFFF;
	font-size: 1.4rem;
	border: none;
	display: block;
	margin: 0 auto;
	margin-top: 3rem;
}
#toForget{
	color: #A9A9A9;
	font-size: 1.2rem;
	margin-top: 1.5rem;
	text-decoration: underline;
	display: flex;
	justify-content: center;
}
#toForget a{
	display: inline;
}

</style>
